home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / DirectSound / EnumDevices / enumdevices.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-31  |  9.4 KB  |  276 lines

  1. //----------------------------------------------------------------------------
  2. // File: EnumDevices.cpp
  3. //
  4. // Desc: This sample shows how to enumerate DirectSound sound and capture 
  5. //       devices.
  6. //
  7. // Copyright (c) 1999-2001 Microsoft Corp. All rights reserved.
  8. //-----------------------------------------------------------------------------
  9. #define STRICT
  10. #include <windows.h>
  11. #include <basetsd.h>
  12. #include <commdlg.h>
  13. #include <mmreg.h>
  14. #include <dxerr8.h>
  15. #include <dsound.h>
  16. #include "resource.h"
  17.  
  18.  
  19.  
  20.  
  21. //-----------------------------------------------------------------------------
  22. // Function-prototypes
  23. //-----------------------------------------------------------------------------
  24. INT_PTR CALLBACK MainDlgProc( HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam );
  25. INT_PTR CALLBACK DSoundEnumCallback( GUID* pGUID, LPSTR strDesc, LPSTR strDrvName,
  26.                                   VOID* pContext );
  27. HRESULT OnInitDialog( HWND hDlg );
  28. HRESULT InitDirectSound( HWND hDlg );
  29. HRESULT FreeDirectSound();
  30.  
  31.  
  32.  
  33. //-----------------------------------------------------------------------------
  34. // Global data
  35. //-----------------------------------------------------------------------------
  36. #define SAFE_DELETE(p)  { if(p) { delete (p);     (p)=NULL; } }
  37. #define SAFE_RELEASE(p) { if(p) { (p)->Release(); (p)=NULL; } }
  38.  
  39. LPDIRECTSOUND        g_pDS        = NULL;
  40. LPDIRECTSOUNDCAPTURE g_pDSCapture = NULL;
  41.  
  42.  
  43.  
  44.  
  45. //-----------------------------------------------------------------------------
  46. // Name: WinMain()
  47. // Desc: Entry point for the application.  Since we use a simple dialog for 
  48. //       user interaction we don't need to pump messages.
  49. //-----------------------------------------------------------------------------
  50. INT APIENTRY WinMain( HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR pCmdLine, 
  51.                       INT nCmdShow )
  52. {
  53.     // Initialize COM
  54.     CoInitialize( NULL );
  55.  
  56.     // Display the main dialog box.
  57.     DialogBox( hInst, MAKEINTRESOURCE(IDD_MAIN), NULL, MainDlgProc );
  58.  
  59.     // Release COM
  60.     CoUninitialize();
  61.  
  62.     return TRUE;
  63. }
  64.  
  65.  
  66.  
  67.  
  68. //-----------------------------------------------------------------------------
  69. // Name: MainDlgProc()
  70. // Desc: Handles dialog messages
  71. //-----------------------------------------------------------------------------
  72. INT_PTR CALLBACK MainDlgProc( HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam )
  73. {
  74.     HRESULT hr; 
  75.  
  76.     switch( msg ) 
  77.     {
  78.         case WM_COMMAND:
  79.             switch( LOWORD(wParam) )
  80.             {
  81.                 case IDC_CREATE:
  82.                     // Init DirectSound
  83.                     if( SUCCEEDED( hr = InitDirectSound( hDlg ) ) )
  84.                     {
  85.                         MessageBox( hDlg, TEXT("DirectSound interface created successfully"), 
  86.                                           TEXT("EnumDevices"), MB_OK );
  87.                     }
  88.                     else
  89.                     {
  90.                         DXTRACE_ERR( TEXT("InitDirectSound"), hr );
  91.                         MessageBox( hDlg, TEXT("DirectSound interface creatation failed"), 
  92.                                           TEXT("EnumDevices"), MB_OK | MB_ICONERROR );
  93.                     }
  94.                     break;
  95.  
  96.                 case IDCANCEL:
  97.                     EndDialog( hDlg, IDCANCEL );
  98.                     break;
  99.  
  100.                 default:
  101.                     return FALSE; // Didn't handle message
  102.             }
  103.             break;
  104.  
  105.         case WM_INITDIALOG:
  106.             if( FAILED( hr = OnInitDialog( hDlg ) ) )
  107.             {
  108.                 DXTRACE_ERR( TEXT("OnInitDialog"), hr );
  109.                 MessageBox( hDlg, "Error enumerating DirectSound devices. "
  110.                                   "Sample will now exit.", "DirectSound Sample", 
  111.                                   MB_OK | MB_ICONERROR );
  112.                 EndDialog( hDlg, IDABORT );
  113.             }
  114.             break;
  115.  
  116.         case WM_DESTROY:
  117.             // Cleanup everything
  118.             FreeDirectSound();
  119.             break; 
  120.  
  121.         default:
  122.             return FALSE; // Didn't handle message
  123.     }
  124.  
  125.     return TRUE; // Handled message
  126. }
  127.  
  128.  
  129.  
  130.  
  131. //-----------------------------------------------------------------------------
  132. // Name: OnInitDialog()
  133. // Desc: Initializes the dialogs (sets up UI controls, etc.)
  134. //-----------------------------------------------------------------------------
  135. HRESULT OnInitDialog( HWND hDlg )
  136. {
  137.     HRESULT hr;
  138.  
  139.     // Load the icon
  140. #ifdef _WIN64
  141.     HINSTANCE hInst = (HINSTANCE) GetWindowLongPtr( hDlg, GWLP_HINSTANCE );
  142. #else
  143.     HINSTANCE hInst = (HINSTANCE) GetWindowLong( hDlg, GWL_HINSTANCE );
  144. #endif
  145.     HICON hIcon = LoadIcon( hInst, MAKEINTRESOURCE( IDR_MAINFRAME ) );
  146.  
  147.     // Set the icon for this dialog.
  148.     PostMessage( hDlg, WM_SETICON, ICON_BIG,   (LPARAM) hIcon );  // Set big icon
  149.     PostMessage( hDlg, WM_SETICON, ICON_SMALL, (LPARAM) hIcon );  // Set small icon
  150.  
  151.     // Enumerate the sound devices and place them in the combo box
  152.     HWND hSoundDeviceCombo = GetDlgItem( hDlg, IDC_SOUND_DEVICE_COMBO );
  153.     if( FAILED( hr = DirectSoundEnumerate( (LPDSENUMCALLBACK)DSoundEnumCallback,
  154.                                            (VOID*)hSoundDeviceCombo ) ) )
  155.         return DXTRACE_ERR( TEXT("DirectSoundEnumerate"), hr );
  156.  
  157.     // Enumerate the capture devices and place them in the combo box
  158.     HWND hCaptureDeviceCombo = GetDlgItem( hDlg, IDC_CAPTURE_DEVICE_COMBO );
  159.     if( FAILED( hr = DirectSoundCaptureEnumerate( (LPDSENUMCALLBACK)DSoundEnumCallback,
  160.                                                   (VOID*)hCaptureDeviceCombo ) ) )
  161.         return DXTRACE_ERR( TEXT("DirectSoundCaptureEnumerate"), hr );
  162.  
  163.     // Select the first device in the combo box
  164.     SendMessage( hSoundDeviceCombo,   CB_SETCURSEL, 0, 0 );
  165.     SendMessage( hCaptureDeviceCombo, CB_SETCURSEL, 0, 0 );
  166.  
  167.     return S_OK;
  168. }
  169.  
  170.  
  171.  
  172.  
  173. //-----------------------------------------------------------------------------
  174. // Name: DSoundEnumCallback()
  175. // Desc: Enumeration callback called by DirectSoundEnumerate
  176. //-----------------------------------------------------------------------------
  177. INT_PTR CALLBACK DSoundEnumCallback( GUID* pGUID, LPSTR strDesc, LPSTR strDrvName,
  178.                                   VOID* pContext )
  179. {
  180.     // Set aside static storage space for 20 audio drivers
  181.     static GUID  AudioDriverGUIDs[20];
  182.     static DWORD dwAudioDriverIndex = 0;
  183.  
  184.     GUID* pTemp  = NULL;
  185.  
  186.     if( pGUID )
  187.     {
  188.         if( dwAudioDriverIndex >= 20 )
  189.             return TRUE;
  190.  
  191.         pTemp = &AudioDriverGUIDs[dwAudioDriverIndex++];
  192.         memcpy( pTemp, pGUID, sizeof(GUID) );
  193.     }
  194.  
  195.     HWND hSoundDeviceCombo = (HWND)pContext;
  196.  
  197.     // Add the string to the combo box
  198.     SendMessage( hSoundDeviceCombo, CB_ADDSTRING, 
  199.                  0, (LPARAM) (LPCTSTR) strDesc );
  200.  
  201.     // Get the index of the string in the combo box
  202.     INT nIndex = (INT)SendMessage( hSoundDeviceCombo, CB_FINDSTRING, 
  203.                                    0, (LPARAM) (LPCTSTR) strDesc );
  204.  
  205.     // Set the item data to a pointer to the static guid stored in AudioDriverGUIDs
  206.     SendMessage( hSoundDeviceCombo, CB_SETITEMDATA, 
  207.                  nIndex, (LPARAM) pTemp );
  208.  
  209.     return TRUE;
  210. }
  211.  
  212.  
  213.  
  214.  
  215. //-----------------------------------------------------------------------------
  216. // Name: InitDirectSound()
  217. // Desc: Initilizes DirectSound
  218. //-----------------------------------------------------------------------------
  219. HRESULT InitDirectSound( HWND hDlg )
  220. {
  221.     HRESULT hr;
  222.  
  223.     // Free any previous DirectSound objects
  224.     FreeDirectSound();
  225.  
  226.     // Get the HWNDs the combo boxes
  227.     HWND hSoundDeviceCombo   = GetDlgItem( hDlg, IDC_SOUND_DEVICE_COMBO );
  228.     HWND hCaptureDeviceCombo = GetDlgItem( hDlg, IDC_CAPTURE_DEVICE_COMBO );
  229.  
  230.     // Get the index of the currently selected devices
  231.     INT nSoundIndex   = (INT)SendMessage( hSoundDeviceCombo,   CB_GETCURSEL, 0, 0 ); 
  232.     INT nCaptureIndex = (INT)SendMessage( hCaptureDeviceCombo, CB_GETCURSEL, 0, 0 ); 
  233.  
  234.     // Get the GUID attached to the combo box item
  235.     GUID* pSoundGUID = (GUID*) SendMessage( hSoundDeviceCombo, CB_GETITEMDATA, 
  236.                                             nSoundIndex, 0 );
  237.     GUID* pCaptureGUID = (GUID*) SendMessage( hCaptureDeviceCombo, CB_GETITEMDATA, 
  238.                                               nCaptureIndex, 0 );
  239.  
  240.     // Create IDirectSound using the select sound device
  241.     if( FAILED( hr = DirectSoundCreate( pSoundGUID, &g_pDS, NULL ) ) )
  242.         return DXTRACE_ERR( TEXT("DirectSoundCreate"), hr );
  243.  
  244.     // Release the IDirectSound object immediately since we don't want
  245.     // to limit this sample to only computers that support full duplex audio
  246.     SAFE_RELEASE( g_pDS ); 
  247.  
  248.     // Create IDirectSoundCapture using the select capture device
  249.     if( FAILED( hr = DirectSoundCaptureCreate( pCaptureGUID, &g_pDSCapture, NULL ) ) )
  250.         return DXTRACE_ERR( TEXT("DirectSoundCaptureCreate"), hr );
  251.  
  252.     // Release g_pDSCapture, since we don't need it really 
  253.     SAFE_RELEASE( g_pDSCapture ); 
  254.  
  255.     return S_OK;
  256. }
  257.  
  258.  
  259.  
  260.  
  261. //-----------------------------------------------------------------------------
  262. // Name: FreeDirectSound()
  263. // Desc: Releases DirectSound 
  264. //-----------------------------------------------------------------------------
  265. HRESULT FreeDirectSound()
  266. {
  267.     // Release DirectSound interfaces
  268.     SAFE_RELEASE( g_pDSCapture ); 
  269.     SAFE_RELEASE( g_pDS ); 
  270.  
  271.     return S_OK;
  272. }
  273.  
  274.  
  275.  
  276.